package com.leetCode.problem;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-11-05 16:59:56
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-11-05     张李鑫                     1.0         1.0 Version
 */
public class Problem36 {

    /**
     * 请你判断一个9x9 的数独是否有效。只需要 根据以下规则 ，验证已经填入的数字是否有效即可。
     *
     * 数字1-9在每一行只能出现一次。
     * 数字1-9在每一列只能出现一次。
     * 数字1-9在每一个以粗实线分隔的3x3宫内只能出现一次。（请参考示例图）
     *row  col
     */

    /**
     * [["5","3",".",".","7",".",".",".","."],
     * ["6",".",".","1","9","5",".",".","."],
     * [".","9","8",".",".",".",".","6","."]
     * ["8",".",".",".","6",".",".",".","3"],
     * ["4",".",".","8",".","3",".",".","1"],
     * ["7",".",".",".","2",".",".",".","6"],
     * [".","6",".",".",".",".","2","8","."],
     * [".",".",".","4","1","9",".",".","5"],
     * [".",".",".",".","8",".",".","7","9"]]
     *
     * @param board
     * @return
     */
    public static boolean isValidSudoku(char[][] arr) {
        boolean[][] rowArr = new boolean[8][10];
        boolean[][] colArr = new boolean[8][10];
        boolean[][] Arr = new boolean[8][10];

        for (int i = 0; i < arr.length; i++) {
            for (int i1 = 0; i1 < arr[i].length; i1++) {
                if (arr[i][i1] != '.') {
                    int bid = 3 * (i / 3) + (i1 / 3);
                    int val = arr[i][i1]-'0';
                    if (rowArr[i][val] || colArr[i1][val] || Arr[bid][val]) {
                        return false;
                    }
                    rowArr[i][val] = true;
                    colArr[i1][val] = true;
                    Arr[bid][val] = true;
                }
            }
        }
        return true;
    }
    public static boolean isValidSudoku(String[][] board) {
        for (int i = 0; i < board.length; i++) {
            for (int i1 = 0; i1 < board[i].length; i1++) {
                if (!rowValid(i, i1, board) || !geValid(i, i1, board) || !colValid(i, i1, board)) {
                    return false;
                }
            }
        }
        return true;
    }


    public static boolean rowValid(int i, int j, String[][] board) {
        for (int i1 = 0; i1 < board[i].length; i1++) {
            if (board[i][j]!="."&&board[i][j] == board[i][i1] && j != i1) {
                return false;
            }
        }
        return true;
    }

    public static boolean colValid(int i, int j, String[][] board) {
        for (int i1 = 0; i1 < board.length; i1++) {
            if (board[i][j]!="."&&board[i1][j] == board[i][j] && i != i1) {
                return false;
            }
        }
        return true;
    }

    public static boolean geValid(int i, int j, String[][] board) {
        int row = i / 3;
        int col = j / 3;
        for (int k = row * 3; k <(row+1)* 3; k++) {
            for (int i1 = col * 3; i1 < (col+1)*3; i1++) {
                if (board[i][j]!="."&&i != k && j != i1 && board[k][i1] == board[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        // [".",".",".",".",".","2",".",".","."],
        // [".","2",".","9",".",".",".",".","."],[".",".","4",".",".",".",".",".","."]]
        String[][] arr = new String[][]{
                {".",".",".",".","5",".",".","1","."},
                {".","4",".","3",".",".",".",".","."},
                {".",".",".",".",".","3",".",".","1"},
                {"8",".",".",".",".",".",".","2","."},
                {".",".","2",".","7",".",".",".","."},
                {".","1","5",".",".",".",".",".","."},
                {".",".",".",".",".","2",".",".","."},
                {".","2",".","9",".",".",".",".","."},
                {".",".","4",".",".",".",".",".","."}
        };
        System.out.println(isValidSudoku(arr));
    }

}
